/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFConexao.java
 *
 * Created on 29/12/2012, 02:06:43
 */
package atualizacoes;

import java.util.Properties;
import java.util.prefs.Preferences;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Douglas
 */
public class JFConexao extends javax.swing.JFrame {

    /** Creates new form JFConexao */
    public JFConexao() {
        initComponents();
    }
    
    public static Properties prefsPU = new Properties();
    public static Preferences prefs = Preferences.userRoot().node(""); //this.getClass().getName()
    
    public static EntityManager getConf(){
        String PU  = prefs.get("unidadePersistencia", null).toString().concat("PU");
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(PU,prefsPU);  
        return emf.createEntityManager();        
    }
    
    public static void setConfiguracao(){
        //Acessar as entidades e deixar em branco o campo: catalog
        String user = prefs.get("usuarioMySQL", null);
        String password = prefs.get("senhaMySQL", null);
        String url = prefs.get("url", null);
        String driver = prefs.get("driver", null );
        boolean lembrarSenha = prefs.get("lembrarSenhaMySQL","" ).equals("Sim");
       
        //Definir variavel prop publica
        prefsPU.setProperty("toplink.jdbc.user", user);
        
        if (lembrarSenha){
          prefsPU.setProperty("toplink.jdbc.password", password );
        }else{
          prefsPU.setProperty("toplink.jdbc.password", "" );
        }
        prefsPU.setProperty("toplink.jdbc.url", url);
        prefsPU.setProperty("toplink.jdbc.driver", driver);
        
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        ipjTextField = new javax.swing.JTextField();
        usuariojTextField = new javax.swing.JTextField();
        senhajPasswordField = new javax.swing.JPasswordField();
        lembrarSenhajCheckBox = new javax.swing.JCheckBox();
        urljTextField = new javax.swing.JTextField();
        nomeBancoDadosjTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        driverjComboBox = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        unidadePersistenciajTextField = new javax.swing.JTextField();
        okjButton = new javax.swing.JButton();
        sairjButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        ipjTextField.setText("mysql01.uni5.net");

        usuariojTextField.setText("facilassim01");

        senhajPasswordField.setText("facilassim01");

        lembrarSenhajCheckBox.setSelected(true);
        lembrarSenhajCheckBox.setText("Lembrar senha");

        urljTextField.setText("jdbc:mysql://localhost:3306/contatospro");

        nomeBancoDadosjTextField.setText("contatospro");

        jLabel1.setText("IP ou Nome do servidor MySQL:");

        jLabel2.setText("Usuário conexão MySQL:");

        jLabel3.setText("Senha:");

        jLabel5.setText("URL Conexão:");

        jLabel6.setText("Nome do banco de dados:");

        jLabel7.setText("Driver do Banco:");

        driverjComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "com.mysql.jdbc.Driver" }));

        jLabel8.setText("Unidade de Persistencia:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ipjTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                    .addComponent(usuariojTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                    .addComponent(urljTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                    .addComponent(nomeBancoDadosjTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                    .addComponent(unidadePersistenciajTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                    .addComponent(driverjComboBox, 0, 302, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(senhajPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lembrarSenhajCheckBox)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ipjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usuariojTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(6, 6, 6)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(senhajPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lembrarSenhajCheckBox)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(urljTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nomeBancoDadosjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(unidadePersistenciajTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(driverjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addContainerGap(41, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Conexão", jPanel1);

        okjButton.setText("OK");
        okjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okjButtonActionPerformed(evt);
            }
        });

        sairjButton.setText("Sair");
        sairjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sairjButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(429, Short.MAX_VALUE)
                .addComponent(okjButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sairjButton)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 523, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okjButton)
                    .addComponent(sairjButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okjButtonActionPerformed
        //Guardar preferencia de dominio
        //Preferences prefs = Preferences.userRoot().node(""); 
        //Guardar preferencia de dominio
        //Preferences prefs = Preferences.userRoot().node(""); //this.getClass().getName()
        prefs.put("usuarioMySQL", usuariojTextField.getText());
        
        if (lembrarSenhajCheckBox.isSelected()){
          prefs.put("senhaMySQL", senhajPasswordField.getText() );
          prefs.put("lembrarSenhaMySQL", "Sim" );
        }else{
          prefs.put("senhaMySQL", "" );
          prefs.put("lembrarSenhaMySQL", "Não" );
        }
        
        prefs.put("nomeBanco", nomeBancoDadosjTextField.getText());
        prefs.put("unidadePersistencia", unidadePersistenciajTextField.getText());
        prefs.put("servidor", ipjTextField.getText());
        prefs.put("driver", driverjComboBox.getSelectedItem().toString());
        prefs.put("url", urljTextField.getText());
        
        
        
        //Definir variavel prop publica
        prefsPU.setProperty("toplink.jdbc.user", usuariojTextField.getText());
        
        if (lembrarSenhajCheckBox.isSelected()){
          prefsPU.setProperty("toplink.jdbc.password", senhajPasswordField.getText() );
        }else{
          prefsPU.setProperty("toplink.jdbc.password", "" );
        }
        prefsPU.setProperty("toplink.jdbc.url", urljTextField.getText());
        prefsPU.setProperty("toplink.jdbc.driver", driverjComboBox.getSelectedItem().toString());
        
        sairjButtonActionPerformed(null);
    }//GEN-LAST:event_okjButtonActionPerformed

    private void sairjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sairjButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_sairjButtonActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        this.setLocationRelativeTo(null);
        //Recuperar prefencia do codigo
        

        ipjTextField.setText( prefs.get("servidor", ipjTextField.getText())  );
        usuariojTextField.setText( prefs.get("usuarioMySQL", usuariojTextField.getText())  );
        String lembrarSenha = prefs.get("lembrarSenhaMySQL","" );
        if ( lembrarSenha.equals("Sim") ){
           senhajPasswordField.setText( prefs.get("senhaMySQL", null) );
        }

        nomeBancoDadosjTextField.setText( prefs.get("nomeBanco", nomeBancoDadosjTextField.getText())  );
        ipjTextField.setText( prefs.get("servidor", ipjTextField.getText())  );
        driverjComboBox.setSelectedItem( prefs.get("driver", driverjComboBox.getSelectedItem().toString() )  );
        urljTextField.setText( prefs.get("url", urljTextField.getText())  );
        unidadePersistenciajTextField.setText( prefs.get("unidadePersistencia", unidadePersistenciajTextField.getText())  );


    }//GEN-LAST:event_formWindowOpened

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFConexao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFConexao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFConexao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFConexao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JFConexao().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox driverjComboBox;
    private javax.swing.JTextField ipjTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JCheckBox lembrarSenhajCheckBox;
    private javax.swing.JTextField nomeBancoDadosjTextField;
    private javax.swing.JButton okjButton;
    private javax.swing.JButton sairjButton;
    private javax.swing.JPasswordField senhajPasswordField;
    private javax.swing.JTextField unidadePersistenciajTextField;
    private javax.swing.JTextField urljTextField;
    private javax.swing.JTextField usuariojTextField;
    // End of variables declaration//GEN-END:variables
}
